package cn.appsys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appsys.biz.CategoryBiz;
import cn.appsys.biz.DictionaryBiz;
import cn.appsys.biz.InfoBiz;
import cn.appsys.biz.ShowInfoBiz;
import cn.appsys.biz.VersionBiz;
import cn.appsys.entity.AppCategory;
import cn.appsys.entity.AppInfo;
import cn.appsys.entity.AppVersion;
import cn.appsys.entity.DataDictionary;
import cn.appsys.entity.ShowInfo;

@Controller
public class ShowAppAuditController {
	@Autowired
	private InfoBiz infoBiz;
	@Autowired
	private ShowInfoBiz showInfoBiz;
	
	@Autowired
	private DictionaryBiz dictionaryBiz;
	
	@Autowired
	private CategoryBiz categoryBiz;
	@Autowired
	private VersionBiz versionBiz;
	
	
	@RequestMapping("/AppAudit")
	public String AppInfo(Model model,String softwareName,Integer status,Integer flatformId,Integer oneselect,
			Integer towselect,Integer threeselect,Integer page){
		page=page==null?1:page;
		status=status==null?-1:status;
		oneselect=oneselect==null?-1:oneselect;
		towselect=towselect==null?-1:towselect;
		threeselect=threeselect==null?-1:threeselect;
		flatformId=flatformId==null?-1:flatformId;
    	int pageSize=4;
    	int startIndex=(page-1)*pageSize;
		List<ShowInfo> showInfoList=showInfoBiz.showInfo(null,1,flatformId, oneselect, towselect, threeselect, startIndex, pageSize);
		int rows=showInfoBiz.getRowsCount(softwareName, status, flatformId, oneselect, towselect, threeselect);
    	int pageCount=(int)Math.ceil((double)rows/pageSize);
		List<DataDictionary> statusList=dictionaryBiz.getByStatus();
		
		List<DataDictionary> flatformList=dictionaryBiz.getByFlatform();
		
		List<AppCategory> listone =categoryBiz.getAllCategory();
		
		model.addAttribute("listone",listone);
		model.addAttribute("flatformList", flatformList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("showInfoList", showInfoList);
		model.addAttribute("totalPages",pageCount);
		model.addAttribute("page",page);
		return "appaudit";
	}
	@RequestMapping(value="jsonAppaudit",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String jsonAppaudit(String softwareName,Integer status,Integer flatformId,Integer oneselect,Integer towselect,Integer threeselect,Integer page){
		page=page==null?1:page;
		status=status==null?-1:status;
		flatformId=flatformId==null?-1:flatformId;
		oneselect=oneselect==null?-1:oneselect;
		towselect=towselect==null?-1:towselect;
		threeselect=threeselect==null?-1:threeselect;
		int pageSize=4;
    	int startIndex=(page-1)*pageSize;
		
		List<ShowInfo> showInfoList=showInfoBiz.showInfo(softwareName, status, flatformId, oneselect, towselect, threeselect, startIndex, pageSize);
		
		return JSON.toJSONString(showInfoList);
	}
	
	@RequestMapping("/auditappview")
	public String auditappview(int id,Model model){
		List<AppVersion> versionList=versionBiz.getByIdVersion(id);
		
		AppInfo appInfo=infoBiz.getByIdAppInfo(id);
		
		String versionName=dictionaryBiz.getByIdVersionStatus(appInfo.getStatus());
		List<AppCategory> listone =categoryBiz.getAllCategory();
		
		model.addAttribute("listone",listone);
		
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("versionName", versionName);
		model.addAttribute("versionList", versionList);
		return "auditappview";
	}
	@RequestMapping("/updatestatusaudit")
	public String updatestatusaudit(int id,int status){
		
		infoBiz.updateStatus(id, status);
		return "redirect:/AppAudit";
	}
	
	
	
	
}
